<?php

namespace app\common\order\logic;

use app\common\BaseLogic;
use app\common\order\model\OrderAdditional;
use app\common\order\model\OrderPay;
use app\common\order\model\OrderProtocol;

class PaymentLogic extends BaseLogic
{
    public function getBrief($orderId) {

        $total = [];
        $model = new OrderPay();
        $rows = $model->where('order_id', $orderId)->select();
        foreach ($rows as $row) {
            array_push($total, $row->total);
        }
        $data = [];
        $data['times'] = count($total);
        $data['collected'] = array_sum($total);

        $totals = $this->getProtocolTotal($orderId) + $this->getAdditionalTotal($orderId);
        $data['uncollected'] = round($totals - $data['collected'], 2);

        return $data;
    }

    public function getProtocolTotal($orderId) {
        $model = new OrderProtocol();

        $where = [];
        $where[] = ['order_id', '=', $orderId];
        $where[] = ['check_status', '<>', 3];
        return $model->where($where)->sum('total');
    }

    public function getAdditionalTotal($orderId) {
        $model = new OrderAdditional();

        $where = [];
        $where[] = ['order_id', '=', $orderId];
        $where[] = ['type', '=', 1];
        $where[] = ['check_status', '<>', 3];
        return $model->where($where)->sum('total');
    }
}